        TITL DAI FIRMWARE 2E407-2E5FB  V1.1
        ORG   :E407
*
*
*
****************************
* SET UP SCREEN FOR MODE 0 *
****************************
*
* Sets up a mode 0 screen, whatever the current mode
* is. If already a character area exists, it is
* moved to the top of the new screen.
*
* Entry: None.
* Exit:  All registers corrupted.
*        CY=0: OK.
*        CY=1: No room.
*
SSM0    STC             CY=1
        PUSH  PSW
        LXI   H,CON0    Startaddr mode 0 table
        CALL  VARS      Load pointers with parameter
        JC    SS020     If no room available
        LXI   D,ERRLIN  Addr text colour table
        PUSH  D         Save addr
        LHLD  SCREEN    Get 1st byte screen RAM
        MVI   B,:06
        CALL  SSUBL     Set up header
        LDA   COL1      Get old screen mode
        CPI   :10       During initialisation ?
        JZ    SS010     Then jump
        RAR             Split screen ?
        JNC   SS010     Jump if not
        CALL  SMVTXT    If split mode: move old
 REM text, cursor, etc
SS010   XCHG            Addr after header in DE
        LHLD  GAE       Get addr end char area
        XCHG
        CALL  FILLS     Blank char area
        CNC   SSETC     Set cursor at begin 1st line
 REM
* Entry from SSMG:
 REM
SS015   POP   D
        POP   PSW
        CMC             CY=0
        RET 
 REM
* If no room available:
* Entry from SSMG, SSM, SSMA:
 REM
SS020   POP   PSW       CY=1
        RET 
*
**********************************
* SET UP SCREEN FOR GRAPHIC MODE *
**********************************
*
* Entry: A:  Screen mode (split if odd).
* Exit:  CY=0: O.K.:
*        Split mode:       DE: Addr text colours.
*        All graphic mode: DE: Addr graph colours.
*        AF preserved. BCHL corrupted.
*        CY=1: Insufficient room.
*
SSMG    STC 
        PUSH  PSW
        MOV   D,A       Screen mode in D
        ANI   :01       Z=1 if full colour mode
        MOV   A,D
        RAR             Disable split mode bit in A
        CNZ   SSMA      If split mode: set up screen
        CZ    SSM       If full colour mode: idem
        JC    SS020     Abort if no room
        POP   PSW       Get mode code
        PUSH  PSW
        PUSH  D
        LXI   D,COL2    Addr COLORG table
        LHLD  SCREEN    Get addr 1st byte screen RAM
        MVI   B,:06     Depth each blanking line
 REM in header -1
        CALL  SSUBL     Set up header with COLORG
 REM colours
        JMP   SS015     Quit, all OK
*
* SET UP A FULL GRAPHIC SCREEN:
*
* Sets up a screen RAM for an all-graphics mode.
*
* Entry: A: Mode code /2.
*        D: Mode code.
* Exit:  CY=0: O.K.:
*              DE points to table graphic colours.
*              AF preserved. BCHL corrupted.
*        CY=1: Insufficient space.
*
SSM     STC 
        PUSH  PSW
        LXI   H,TABM    Addr table vectors full
 REM graphic mode
        CALL  TABP      Set up screen mode
        JC    SS020     Jump if no room
        LDA   COL1      Get current screen mode
        SUB   D         ) Check if change split
        DCR   A         ) to all graphics
        JZ    SSM20     Then check if sufficient
 REM RAM available and change
 REM mode
        CALL  CURDEL    Delete cursor
        LDA   GRL       Get nr of graphics lines
        MOV   C,A       in C
        LHLD  SCTOP     Get addr top graph area
        CALL  SGINIT    Blank whole screen
SSM10   LXI   D,COL2    Addr COLORG table
        POP   PSW
        CMC             CY=0: O.K.
        RET 
 REM
* Change from split to all-graphic mode:
 REM
SSM21   JNC   SMA30     Set up screen mode
        CALL  CURDEL    Delete cursor
        LHLD  GRE       Get addr temp save area
        MOV   B,H       ) in BC
        MOV   C,L       )
        PUSH  B         Save it on stack
        LHLD  GAS       Get startaddr archive
 REM area
        XCHG            in DE
        LHLD  GAE       Get addr end archive area
        CALL  MOVES     Move archive area into
 REM temp save area
        LHLD  GRR       Get addr top of rolled area
        MOV   B,H       ) in BC
        MOV   C,L       )
        LHLD  SCTOP     Get addr top old graphics
        XCHG            in DE
        LHLD  GREG      Get end old screen
        CALL  MOVES     Move lower part screen
 REM downwards
        MOV   B,D       ) BC is addr where to put
        MOV   C,E       ) archive area
        POP   D         Get startaddr temp save area
        LHLD  GTE       Get end temp save area
        CALL  MOVES     Move temp save area to
 REM top of screen
        JMP   SSM10     Quit
*
* SET UP SCREEN FOR SPLIT MODE:
*
* Sets up a split screen for a given mode in the
* lower RAM.
*
* Entry: A: Mode code /2.
*        D: Mode code.
* Exit:  CY=0: O.K.:
*              DE: Address text colour table.
*              AF preserved, BCHL corrupted.
*        CY=1: Insufficient space.
*
SSMA    STC 
        PUSH  PSW
        LXI   H,TABMA   Startaddr table vectors
 REM split modes
        CALL  TABP      Set up screen mode
        JC    SS020     Abort if insufficient space
        LDA   COL1      Get old screen mode
        SUB   D         ) Check if change from
        INR   A         ) all-graph to split
        PUSH  PSW       Preserve flags
        PUSH  D         and new mode code
        JNZ   SMA10     If not splitting old mode:
 REM clear graph and moved areas
        CALL  SPTA2     Check suff RAM available;
 REM prepare full graphic mode
        NOP 
        JNC   SMA20     Set up current mode if
 REM not O.K.
        LHLD  GAS       Get start temp save area
        MOV   B,H       ) in BC
        MOV   C,L       )
        LHLD  SCTOP     Get addr after header
        XCHG            in DE
        LHLD  GRR       Get addr top of screen
        CALL  MOVES     Move top of screen into
 REM temp save area
        MOV   B,D       ) BC is addr top of screen
        MOV   C,E       )
        XCHG            Addr top rolled up area
        LHLD  GREG      Get previous end of graphics
        CALL  MOVES     Move lower part of screen
        LHLD  SCE       Get final place for archive
 REM code
        MOV   B,H       ) in BC
        MOV   C,L       )
        LHLD  GAS       Get addr start temp. save
 REM area
        XCHG            in DE
        LHLD  GTE       Get addr end split mode
        CALL  MOVES     Move temp save area into
 REM archive area
SMA10   LHLD  CHS       Get start addr char area
        XCHG            in DE
        LHLD  GAE       Get addr end char area
        LDA   COL1      Get current screen mode
        RAR             Check mode
        MVI   C,:04     Nr of char lines in A-mode
        XCHG
        CNC   FILLS     Blank char area
        CNC   SSETC     Cursor on begin of line
        CC    SMVTXT    Find old text and move it
        POP   D
        LHLD  SCTOP     Get addr after header
        LDA   GRL+1     Get nr saved graphics lines
        MOV   C,A       in C
        LDA   GRL       Get nr of graphics lines
        SUB   C         minus saved ones
        MOV   C,A       stored in C
        POP   PSW
        CNZ   SGINIT    Blank visible graph area
        LHLD  SCE       Get addr end of screen
        LDA   GRL+1     Get nr saved graphics lines
        MOV   C,A       in C
        CNZ   SGINIT    Blank saved graph area
        LXI   D,ERRLIN  Addr text colour table
        MVI   B,:00     Middle as narrow as possible
        LHLD  GRE       Get addr middle area
        POP   PSW       Get mode code
        CALL  SSUBL     Set up middle area
 REM (blanking)
        CMC 
        RET 
*
* SET UP SCREEN FOR MODE:
*
* Selects the right table according to the mode
* number and sets the screen variables.
*
* Entry: A:  Mode code /2.
*        HL: Points to screen parameter vectors
*            for each pair of modes.
* Exit:  CY=0: O.K.:
*              AFHL corrupted, BCDE preserved.
*        CY=1: Insufficient space.
*
TABP    ANI   :0E       Bits 1,2,3 only
        CALL  DADA      Add offset to start table
        NOP 
        NOP 
        NOP 
        MOV   A,M       )
        INX   H         ) Get addr from table in HL
        MOV   H,M       )
        MOV   L,A       )
*
* LOAD POINTERS WITH SCREEN PARAMETERS:
*
* Set up vector area 0084-0098 with variables
* describing the current state of the screen
* in the current mode.
*
VARS    STC 
        PUSH  PSW
        PUSH  B
        PUSH  D
        PUSH  H
        PUSH  H
        PUSH  H
        LHLD  SCREEN    Get addr 1st byte screen RAM
        MOV   B,H       in BC
        MOV   C,L
        POP   H         Get startaddr table
        MOV   A,C       )
        SUB   M         )
        MOV   E,A       ) Calc end area used in new
        INX   H         ) mode. Store it in DE.
        MOV   A,B       )
        SBB   M         )
        MOV   D,A       )
        LXI   H,:0000
        JC    VRS05     Jump if insufficient space
        XCHG
VRS05   STC 
        CALL  SMKRM     Make room for new mode
        JNC   VRS30     Jump if no room available
        LHLD  GRE       Get old addr after end
 REM graphics area
        SHLD  GREG      and save it
        LHLD  CHS       Get old startaddr char area
        SHLD  CHSO      and save it
 REM
* Set up area 0084-0093:
 REM
        LXI   D,FFB     Start of variables which
 REM need offsets
        MVI   L,:08     Nr of pointers to be set
VRS10   XTHL            Get addr screen parameters
        MOV   A,C
        SUB   M         Calc lobyte
        STAX  D         And store it in pointer
        INX   H         Next byte
        INX   D
        MOV   A,B
        SBB   M         Calc hibyte
        STAX  D         And store it in pointer
        INX   H
        INX   D
        XTHL
        DCR   L         Decr counter
        JNZ   VRS10     Next parameter
 REM
* Set up area 0094-0098:
 REM
        POP   H         Get addr 1st parameter
        MVI   B,:05     Nr unadjusted constant bytes
VRS20   MOV   A,M       Get parameter
        STAX  D         and store it in pointer
        INX   H
        INX   D
        DCR   B         Decr counter
        JNZ   VRS20     Next parameter
        JMP   XRCC      Popall, CY=0, ret
 REM
* If no room available:
 REM
VRS30   POP   H
        JMP   XRET      Popall (CY=1), ret
*
* VECTORS TO TABLES SCREEN PARAMETERS:
*
* The startaddresses of the tables with para-
* meters for the graphic modes are given.
*
TABM    DBL   CON1      mode 1/2
        DBL   CON3      mode 3/4
        DBL   CON5      mode 5/6
*
TABMA   DBL   CON1A     mode 1A/2A
        DBL   CON3A     mode 3A/4A
        DBL   CON5A     mode 5A/6A
*
*************************************
* PERFORM MEMORY MANAGEMENT ROUTINE *
*************************************
*
* Entry: HL points to last free byte in RAM.
*
SMKRM   INX   H
        PUSH  H
        LHLD  ASMKRM    Get addr mem.management
 REM routine
        XTHL            Put it on stack
        RET             Perform this routine and
 REM return afterwards to origin.
 REM returnaddress.
*
*********************************
* SET UP AN EMPTY GRAPHICS AREA *
*********************************
*
* Initialises an area of the screen into graphic
* state and blanks it. In 16-colour modes, all
* pixels are set 'on'. The foreground colour is
* the first COLORG colour, the background is black.
*
* Entry: D:  Mode code.
*        C:  Number of graphic lines (1-256).
*        HL: Start of area.
* Exit:  All registers preserved.
*
SGINIT  PUSH  PSW
        PUSH  B
        PUSH  D
        PUSH  H
        MOV   A,D       Mode in A
        PUSH  H
        LXI   D,:0000   8 blobs 1st graph colour
        MVI   L,:00     Control byte: graph, low
 REM def, 4-colour
        RAR 
        RAR 
        JC    SGI10     Jump if 4-colour mode
 REM
* 16-colour mode only:
 REM
        PUSH  PSW
        LDA   COL2      Get 1st colour
        ADD   A         )
        ADD   A         ) Move lonibble into
        ADD   A         ) hinibble
        ADD   A         )
        MOV   D,A       Result in D
        MVI   E,:FF     8 blobs foreground
        POP   PSW
        MVI   L,:80     Control byte: graph, low
 REM def, 16-colour
SGI10   RAR 
        JC    SGI20     Jump if mode 3/4
 REM
* Mode 1/2 and 5/6 only:
 REM
        RAR 
        MVI   H,:0B     Low def fields/line
        MVI   A,:03     Low def bit mask
        JNC   SGI30     Jump if mode 1/2
 REM
* Mode 5/6 only:
 REM
        MVI   H,:2C     Super def fields/line
        MVI   A,:20     Super def bit mask
        JMP   SGI30
 REM
* Mode 3/4 only:
 REM
SGI20   MVI   H,:16     High def fields/line
        MVI   A,:11     High def bit mask
*
SGI30   ORA   L         Add def bits to get mode
 REM code
        MOV   B,A
        MOV   A,H       Line length in A
        POP   H         Get top of area
SGI50   PUSH  PSW       Save line length
        MOV   M,B       Load line control byte
        DCX   H
        MVI   M,:40     Null line colour byte
        DCX   H
SGI60   MOV   M,E       ) Load screen data locations
        DCX   H         ) with 1 blank field
        MOV   M,D       )
        DCX   H
        DCR   A         Next screen location
        JNZ   SGI60     Jump if line not ready
        POP   PSW       Restore nr of locations in A
        DCR   C         Next screen line
        JNZ   SGI50     Jump if not ready
        JMP   XRET      Popall, ret
*
*
*
        UNL
CON0    EQU   :E030
ERRLIN  EQU   :007C
SCREEN  EQU   :0080
SSUBL   EQU   :E5FC
COL1    EQU   :009D
SMVTXT  EQU   :E635
GAE     EQU   :008C
FILLS   EQU   :E1FD
SSETC   EQU   :E687
COL2    EQU   :009E
SSM20   EQU   :D700
CURDEL  EQU   :E36B
GRL     EQU   :0096
SCTOP   EQU   :0082
SMA30   EQU   :D70F
GRE     EQU   :0088
GAS     EQU   :0092
MOVES   EQU   :E6C2
GRR     EQU   :0086
GREG    EQU   :0099
GTE     EQU   :0090
SPTA2   EQU   :D706
SMA20   EQU   :D70D
SCE     EQU   :008E
CHS     EQU   :008A
DADA    EQU   :E701
CHSO    EQU   :009B
FFB     EQU   :0084
XRCC    EQU   :E12E
XRET    EQU   :E138
CON1    EQU   :E045
CON3    EQU   :E06F
CON5    EQU   :E099
CON1A   EQU   :E05A
CON3A   EQU   :E084
CON5A   EQU   :E0AE
ASMKRM  EQU   :00C4
        LST
        END 
